<?

class holiday_references {
	
	
	var $holiday_keys = array(
		'x0',
		'x1',
		'x2',
		'x3',
		'x4',
		'x5',
		'tour_op',
		'departure_date',
		'departure_airport',
		'destination_airport',
		'depart_dept_time', 
		'arrive_dept_time',
		'return_date',
		'return_airport',
		'nights',
		'x15',
		'price_adult',
		'price_child',
		'x18',
		'x19',
		'hotel_rating',
		'arrive_dest_time',
		'depart_dest_time',
		'flight_out_no',
		'flight_ret_no',
		'travel_code',
		'resort_code',
		'resort',
		'hotel_code',
		'hotel_name',
		'board_basis',
		'holiday_code',
		'x32',
		'x33',
		'brochure_code',
		'brochure_page',
		'x36',
		'x37',
		'x38',
		'x39',
		'x40',
		'pax_adult', 
		'pax_child', 
		'pax_infant',
		'late_flag'
		);
	

// build holiday array with associative keys from array
function build_holiday_keys($holiday_result){

	if(!empty($holiday_result)){
		return align_keys($this->holiday_keys,$holiday_result);
	}

}


// Function // Search for a booking using the booking id
function get_holiday($reference_id, $cache = false){

	if(!empty($reference_id)){
	
		$params = in($params);
	
		$sql = "SELECT string
		FROM holiday_references 
		WHERE reference_id = '".$reference_id."'
		LIMIT 1 ";

		if($result = apply_query($sql, $num_rows, $cache['cache'])){
		
			$holiday_string = $result[0]['string'];
			
			$holiday_array = array();
			$holiday_array = $this->build_holiday_keys(explode('~',$holiday_string));
			$holiday_array['reference_id'] = $reference_id;
			$holiday_array['string'] = $holiday_string;
			
			return $holiday_array;
		
		}else{
			
			return false;
			
		}
		
	}

}





// FUNCTION // Insert request reference
function insert_reference($holiday_string){

	if(!empty($holiday_string)){

		$holiday_string = in($holiday_string);
		
		$sql = "INSERT IGNORE INTO holiday_references
		SET string = '".trim($holiday_string)."' "; 
	
		$result = mysql_query($sql) or die (mysql_error());
		
		return mysql_insert_id();
		
	}

}



// FUNCTION // update a holiday reference. used in the special offers section for updating an offer
function update_reference($params){

	if(!empty($params)){
	
		$params = in($params);
		
		$sql = "UPDATE holiday_references
		SET string = '".$params['string']."' 
		WHERE reference_id = '".$params['reference_id']."' ";
	
		$result = mysql_query($sql) or die (mysql_error());
		
	
	}

}


// FUNCTION // Find an identical reference
function find_identical_reference($holiday_string, $cache){

	if(!empty($holiday_string)){
		
		$holiday_string = trim(in($holiday_string));
		
		$sql = "SELECT reference_id FROM holiday_references
		WHERE string = '".$holiday_string."'
		LIMIT 1 ";
		
		return apply_query($sql, $num_rows, $cache);
	}

}



// Function // Find top bookings
function search_top_bookings($params){


	$sql = "SELECT SUBSTRING_INDEX( SUBSTRING_INDEX( STRING ,  '~', 10 ) ,  '~' , -1 ) AS destination_airport, COUNT(SUBSTRING_INDEX( SUBSTRING_INDEX( STRING ,  '~', 10 ) ,  '~' , -1 )) AS count
	FROM holiday_references
	WHERE reference_date BETWEEN '".date('Y-m-d', strtotime("-14 days"))."' AND NOW()
	GROUP BY SUBSTRING_INDEX( SUBSTRING_INDEX( STRING ,  '~', 10 ) ,  '~' , -1 ) 
	LIMIT 20";
	
	return apply_query($sql);

}










// FUNCTION // Clean up old references
function clean_up_references($expiry_timestamp){

	if($expiry_timestamp){
		
		// Deleted holidays where reference date is older then given date
		$sql = "DELETE FROM holiday_references
		WHERE reference_date < '".$expiry_timestamp."' ";
		
		$result = mysql_query($sql) or die (mysql_error());
		
		// Deleted holidays where departure date has passed
		$sql = "DELETE FROM holiday_references
		WHERE SUBSTRING_INDEX( SUBSTRING_INDEX( STRING ,  '~', 8 ) ,  '~' , -1 ) < '".date('ymd',strtotime('-1 day'))."' "; // Maintain older references for Google cache???
		
		$result = mysql_query($sql) or die (mysql_error());
		
		/*
		xeround does not support this
		
		// Optomise table ITS MASSIVE!
		$sql = "OPTIMIZE TABLE holiday_references";
		
		$result = mysql_query($sql) or die (mysql_error());
		*/
		
	}

}


// FUNCTION // Delete a reference
function delete_reference($params){

	if(!empty($params['reference_id'])){
		
		// Deleted holidays where reference date is older then given date
		$sql = "DELETE FROM holiday_references
		WHERE reference_id = ".$params['reference_id']."
		LIMIT 1 ";
		
		$result = mysql_query($sql) or die (mysql_error());
	
	}


}



}
?>